package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Write a description of class Dvd here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Dvd
{
    private String barcode;
    private String title;
    private String artist;
    private String publicationDate;
    private ArrayList<Copy> listOfCopies;

    public Dvd(String barcode, String title, String artist, String publicationDate)
    {
        this.barcode = barcode;
        listOfCopies = new ArrayList();
        if (title.length()<3)
        {
            System.out.println("The title has to be longer than 3 characters");
            title= null;
        }
        else
        {
            this.title = title;
        }

        if (artist.length()<3)
        {
            System.out.println("The artist has to be longer than 3 characters");
            artist= null;
        }
        else
        {
            this.artist = artist;
        }
        this.publicationDate = publicationDate;
    }

    public void create(String barcode, String title, String artist, String publicationDate)
    {
        this.barcode = barcode;
        this.title = title;
        this.artist = artist;
        this.publicationDate = publicationDate;
    }

    public void setBarcode(String barcode)
    {
        this.barcode = barcode;
    }

    public String getBarcode()
    {
        return barcode;
    }

    public void setPublicationDate(String publicationDate)
    {
        this.publicationDate = publicationDate;
    }

    public String getPublicationDate()
    {
        return publicationDate;
    }

    public void setTitle(String title)
    {
        this.title=title;
    }

    public String getTitle()
    {
        return title;
    }

    public void setArtist(String artist)
    {
        this.artist=artist;
    }

    public String getArtist()
    {
        return artist;
    }

    public void createCopy(String serialNumber, String purchaseDate, String purchasePrice)
    {
        if(!search(serialNumber))
        {
            Copy c = new Copy(serialNumber, purchaseDate, purchasePrice);
            listOfCopies.add(c);
        }
    }

    public Copy findCopy(String serialNumber)
    {
        int index = 0;
        boolean found = false;
        while(index < listOfCopies.size() && !found)
        {
            if(listOfCopies.get(index).getSerialNumber().equals(serialNumber))

            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if (found ==true)
        {
            return listOfCopies.get(index);
        }
        else 
        {
            return null;
        }
    }

    public void removeCopy(String serialNumber)
    {
        Iterator<Copy> it = listOfCopies.iterator();
        while(it.hasNext())
        {
            Copy t = it.next();
            String serialNumberOfCopy = t.getSerialNumber();
            if(serialNumberOfCopy.equals(serialNumber))
            {
                it.remove();
            }
        }
    }

    public ArrayList<Copy> getList()
    {
        return listOfCopies;
    }

    public void printCopyList()
    {
        for(Copy b:listOfCopies)
        {
            System.out.println("Serial Number: " + b.getSerialNumber());
        }
    }

    private boolean search(String serialNo)
    {    
        for (Copy copy : listOfCopies)
        {
            if (copy.getSerialNumber().equals(serialNo)) 
            {
                return true;
            }
        }
        return false; 
    }
}